package com.jof.framework.template;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jof.framework.SysException;

import freemarker.template.Configuration;
import freemarker.template.TemplateException;

/**
 * 基于Freemarker模板实现的解析器
 * @author Jonathan
 *
 */
public class FreemarkerTemplateEngine implements TemplateEngine {
	private static final Logger LOGGER = LoggerFactory.getLogger(FreemarkerTemplateEngine.class);
	private Configuration freeMarkerconfiguration;
	
	public void setFreeMarkerconfiguration(Configuration freeMarkerconfiguration) {
		this.freeMarkerconfiguration = freeMarkerconfiguration;
	}

	
	public String getText(String templateId, Map<?, ?> parameters) {
		StringWriter stringWriter = new StringWriter();
		try {
			freeMarkerconfiguration.getTemplate(templateId).process(parameters, stringWriter);
		} catch (TemplateException e) {
			LOGGER.error(e.toString());
			throw new SysException(e);
		} catch (IOException e) {
			LOGGER.error(e.toString());
			throw new SysException(e);
		}
		return stringWriter.toString();
	}

}
